from abstract_node.definition import AbstractNode
from abstract_node.output_interfaces import OutputInterfaceToConsole
from abstract_node.output_layer import AbstractOutputLayer
from abstract_fusion_node.input_layer import AbstractInputLayer

import vtk
from vtk.util.colors import red, green, blue

class SmartAbstractFusionNode(AbstractNode):

    def __init__(self, outputDataSheet, inputDataSheet, appName, outputLayer=[], logger=[]):
        # Definition of a default network in case of missing user defined output
        # functions
        if outputLayer!=[]:
            AbstractNode.__init__(self, 
                                  eventDataSheet=outputDataSheet,
                                  appName=appName,
                                  outputLayer=outputLayer,
                                  logger=logger)
        else:
            # Standard Output Definition of the Abstract Sensor Node
            MyNetworkOutput=OutputInterfaceToFAMOUSO(outputDataSheet)
            MyConsolOutput=OutputInterfaceToConsole(outputDataSheet)
            
            MyAbstractOutputLayer=AbstractOutputLayer([MyNetworkOutput, 
                                                       MyConsolOutput])
            AbstractNode.__init__(self, 
                                  eventDataSheet=outputDataSheet,
                                  appName=appName,
                                  outputLayer=MyAbstractOutputLayer,
                                  logger=logger) 
               
        self.sensorProperties=sensor_interface.getproperties()
        if self.channelProperties.period!=0:
            self.input_layer = AbstractInputLayer(sensor_interface,
                                                  mapping_fct=self.input_event_mapping,
                                                  filtering_fct=self.input_event_filtering)           
        else:                
            self.input_layer = AbstractInputLayer(sensor_interface,
                                                  mapping_fct=self.input_event_mapping,
                                                  filtering_fct=self.input_event_filtering,
                                                  trigger_fct=self.dispatcher.trigger)


    def startMeasurements(self):
        self.input_layer.start_measurements()
        
    def stopMeasurements(self):
        self.input_layer.stop_measurements()

    def __repr__(self):
        output= "\n-----------------------------------------------------\n"
        output+="Application name     %s\n"%self.appName
        output+= "Output Subject       \"%s\"" %self.channelProperties.subject
        if self.channelProperties.period!=0:
            output+=" (Period %s) \n" %self.channelProperties.period
        else:
            output+=" (Event based publication)\n"
        output+="-----------------------------------------------------\n"
        output+="Channel Datasheet    %s\n" %self.channelProperties.channelDataSheet    
        output+="Format               %s entries\n"%(len(self.channelProperties.eventFormat))
        for entry in self.channelProperties.eventFormat:
            output+="                     {0:12}".format(entry["Name"])
            output+=" - {0:8}".format(entry["DataType"])
            output+=" - {0:2} ".format(entry["Size"])
            if entry["Static"]==True:
                output+="- static\n"
            else:
                output+="- variable\n"
        output+="-----------------------------------------------------\n"
        if self.sensorProperties.sensorDataSheet!=[]:
            output+="Sensor Datasheet     %s \n" %self.sensorProperties.sensorDataSheet
            output+="Format               %s entries\n"%(len(self.sensorProperties.eventFormat))
        for entry in self.sensorProperties.eventFormat:
            output+="                     {0:12}".format(entry["Name"])
            output+=" - {0:8}".format(entry["DataType"])
            output+=" - {0:2} ".format(entry["Size"])
            if entry["Static"]==True:
                output+="- static\n"
            else:
                output+="- variable\n"
        if self.sensorProperties.omnidirectional==True:
            output+="                     Omnidirectional"        
        if self.sensorProperties.omnidirectional==False:
            output+="                     directed sensor - " 
        if self.sensorProperties.beams!=[]:
            output+="Sensor beams loaded (%d)\n"%len(self.sensorProperties.beams)
        output+="                     measurement period %s\n"% self.sensorProperties.period     
        
        return output